package com.sky.happygos.ui.activity.fragment;

import android.content.Intent;
import android.graphics.ColorFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.facebook.drawee.view.SimpleDraweeView;
import com.sky.happygos.R;
import com.sky.happygos.YiPush.PushService;
import com.sky.happygos.base.ImagePath;
import com.sky.happygos.common.Constants;
import com.sky.happygos.common.LoadView;
import com.sky.happygos.event.ChartOrderSimpleE;
import com.sky.happygos.event.ShopE;
import com.sky.happygos.event.UpdateUserE;
import com.sky.happygos.event.UserMeE;
import com.sky.happygos.model.Shop;
import com.sky.happygos.model.User;
import com.sky.happygos.present.OrderP;
import com.sky.happygos.present.UserP;
import com.sky.happygos.ui.activity.ShopEditA;
import com.sky.happygos.ui.activity.ShopEditA_;
import com.sky.happygos.ui.activity.StatisticsA_;
import com.sky.happygos.ui.activity.UserInfoA_;


import com.sky.happygos.ui.activity.StatisticsA;
import com.sky.happygos.ui.activity.UserInfoA;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.w3c.dom.Text;

import java.util.List;

/**
 * Created by qqqqqqq on 2017/4/14.
 */
@EFragment(R.layout.fragment_person)
public class PersonF extends Fragment {

    @ViewById(R.id.sdv_user_icon)
    SimpleDraweeView icon;
    @ViewById(R.id.sdv_shop_open_icon)
    SimpleDraweeView openIcon;
    @ViewById(R.id.tv_user_name)
    TextView name;
    @ViewById(R.id.tv_user_info)
    TextView info;
    @ViewById(R.id.tv_btn_open)
    TextView btnOpen;

    @ViewById(R.id.tv_today_money)
    TextView todayMoney;
    @ViewById(R.id.tv_today_order)
    TextView todayOrder;

    @Bean
    OrderP orderP;

    @Bean
    UserP userP;

    User user;

    @AfterViews
    public void init(){
        userP.getUser();
        orderP.getChartSimple(0);
    }

    @Click(R.id.user_box)
    public void clickUserInfo(){
        UserInfoA_.intent(getActivity()).start();
    }

    @Click(R.id.ll_user_today_box)
    public void clickDataToday(){
        StatisticsA_.intent(getActivity()).start();
    }

    @Click(R.id.rl_data_all)
    public void clickDataAll(){
        StatisticsA_.intent(getActivity()).start();
    }


    @Click(R.id.rl_shop_edit)
    public void clickShopEdit(){
        ShopEditA_.intent(getActivity()).start();
    }

    @Click(R.id.tv_btn_open)
    public void clickOpenBtn(){
        if(btnOpen.isSelected()){
            btnOpen.setText("开始营业");
        }else{
            btnOpen.setText("停止营业");
        }
        openIcon.setSelected(!btnOpen.isSelected());
        btnOpen.setSelected(!btnOpen.isSelected());

        send(btnOpen.isSelected());
    }

    public void send(boolean open){
        Intent intent = new Intent();
        if(open) {
            intent.setAction(Constants.ACTION_OPEN);
        }else{
            intent.setAction(Constants.ACTION_CLOSE);
        }
        getActivity().sendBroadcast(intent);
    }


    @Subscribe
    public void chartSimpleResult(ChartOrderSimpleE e){
        if(e.isSuccess){
            if(e.type == 0){
                todayOrder.setText(String.format("%d", e.data.count));
                todayMoney.setText(String.format("%.2f", e.data.price));
            }
        }else{
            Toast.makeText(getActivity(),"请求失败",Toast.LENGTH_SHORT).show();
        }
    }

    public void loadUser(){
        icon.setImageURI(ImagePath.to(user.icon));
        if(user.name != null && user.name.length() != 0) {
            name.setText(user.name);
        }else{
            name.setText(user.phone);
        }
        info.setText(user.info);
    }

    @Subscribe
    public void getMe(UserMeE e){
        if(e.isSuccess){
            user = e.data;
            loadUser();
        }else{
            Toast.makeText(getActivity(),"请求错误",Toast.LENGTH_SHORT).show();
        }
    }


    @Subscribe
    public void updateUserResult(UpdateUserE e){
        if(e.isSuccess){
            user = e.data;
            loadUser();
        }
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



}
